package com.yzf.demo.demo.Thread.ThreadPool;

import java.util.Date;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;

/**
 * Created by 于占峰 on 2020/4/4/004.
 * 继承ScheduledThreadPoolExecutor  重写before after 实现监控
 */
public class ScheduledThreadPoolExecutorSelf extends ScheduledThreadPoolExecutor {
    public ScheduledThreadPoolExecutorSelf(int corePoolSize) {
        super(corePoolSize);
    }

    public ScheduledThreadPoolExecutorSelf(int corePoolSize, ThreadFactory threadFactory) {
        super(corePoolSize, threadFactory);
    }

    public ScheduledThreadPoolExecutorSelf(int corePoolSize, RejectedExecutionHandler handler) {
        super(corePoolSize, handler);
    }

    public ScheduledThreadPoolExecutorSelf(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, threadFactory, handler);
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        // System.out.println("线程开始执行时间:"+new Date());
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        System.out.println("初始线程数:" + this.getPoolSize());
        System.out.println("核心线程数:" + this.getCorePoolSize());
        System.out.println("正在执行的任务数量:" + this.getActiveCount());
        System.out.println("已经执行的任务数量:" + this.getCompletedTaskCount());
    }
}
